import { Component, OnInit } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { HttpServcie } from '../../../service/http.service';
import { Config } from '../../../service/config';
import { SysAuthorBean } from './bean/sysAuthor.bean';
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { NzModalService, NzModalSubject } from 'ng-zorro-antd';

@Component({
  selector: 'function-page',
  templateUrl: 'function.html',
  styleUrls: ['function.scss']
})
export class FunctionPage implements OnInit {
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService
  ) { }
  //接收参数
  sysAuthorBean: SysAuthorBean = new SysAuthorBean();
  sysAuthorBeanTotal: SysAuthorBean = new SysAuthorBean();
  //返回的集合
  list: any = [];
  //总页数
  total: string;
  //多选框
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  //系统类型
  searchOptions: any = Config.SYS_TYPE;
  //权限类型
  authorOptions: any = Config.AUTHOR_TYPE;
  //设置add的
  isAddVisible = false;
  isAddConfirmLoading = false;
  inValue: string;
  currentModal: NzModalSubject;
  isConfirmLoading = false;
  modelTitle = "新增";
  updateData: any={}
  modal: any=0;
  totalList: any=[];
  sureDel = false;
  ngOnInit() {
    this.sysAuthorBean.pageSize = 10;
    this.sysAuthorBean.currentPage = 1;
    this.query();

    this.sysAuthorBeanTotal.pageSize = 999999;
    this.sysAuthorBeanTotal.currentPage = 1;
    this.queryAll();
  }

  //查询
  query() {
    this.httpServcie.querySystemAuthorList(this.sysAuthorBean).subscribe(data => {
      if (data.status === 200) {
        this.list = data.data.list;
        this.total = data.data.total;
      } else if (data.status === 401) {
        this.router.navigateByUrl('/login');
        return;
      } else {
        // 异常
        this._notification.create('error', '错误提示', data.message);
      }
    }, error => {
      console.log(JSON.stringify(error))
    })
  }

  //查询
  queryAll() {
    this.httpServcie.querySystemAuthorList(this.sysAuthorBeanTotal).subscribe(data => {
      if (data.status === 200) {
        this.totalList = data.data.list;
      } else {
        // 异常
        //this._notification.create('error', '错误提示', data.message);
      }
    }, error => {
      console.log(JSON.stringify(error))
    })
  }

  // 查询面板
  panels = {
    active: true,
    name: '查询',
    disabled: false,
    childPanel: [
      {
        active: false,
        name: '查询'
      }
    ]
  }

  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.list.every(value => value.disabled || value.checked);
    const allUnChecked = this.list.every(value => value.disabled || !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => data.checked = false);
    }
  };


  //分页
  nzPageSizeChange(event) {
    this.sysAuthorBean.pageSize = event;
    this.sysAuthorBean.currentPage = 1;
    this.query();

  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.sysAuthorBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.sysAuthorBean.currentPage = event;
    this.query();
  }

  //添加
    //this.isAddVisible = true;
  add(titleTpl, contentTpl, footerTpl) {
      this.modelTitle = '添加';
      this.modal = 0;
      
      this.currentModal = this.modalService.open({
        title       : titleTpl,
        content     : contentTpl,
        footer      : footerTpl,
        maskClosable: false,
        style : {
          top: '20px'
        },
        onOk() {
          console.log('Click ok');
        }
      });
    }

  //修改
  update(titleTpl, contentTpl, footerTpl) {
    this.modelTitle = '修改';
    this.modal = 1;
    const updateList: any = [];
    
    this.list.forEach(element => {
      if (element.checked) {
        updateList.push(element);
        this.updateData = element;
        this.updateData.authors = this.totalList;
      }
    });
    if (updateList.length > 1) {
      // 异常
      this._notification.create('warning', '警告提示', '只能选中一条数据！');
      return;
    }
    if (updateList.length == 0) {
      // 异常
      this._notification.create('warning', '警告提示', '请选中一条数据！');
      return;
    }

    this.currentModal = this.modalService.open({
      title       : titleTpl,
      content     : contentTpl,
      footer      : footerTpl,
      maskClosable: false,
      style : {
        top: '20px'
      },
      onOk() {
        console.log('Click ok');
      }
    });

  }

  //添加添加页面的数据
  handleOk(e){
    this.isConfirmLoading = true;
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.isConfirmLoading = false;
      this.currentModal = null;
    }, 1000);
    // this.isAddConfirmLoading = true;
    // this.inValue = "aaaaa";
  }


  //关闭model框
  handleCancel = (e) => {
    console.log(e);
    this.isAddVisible = false;
  }

  //添加后返回的结果
  addOutPut(event) {
    this.isConfirmLoading = true;
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.isConfirmLoading = false;
      this.currentModal = null;
    }, 500);
    this.query();
    this.queryAll();
  }

  delete(){

    const deleteList: any = [];
    this.list.forEach(element => {
      if (element.checked) {
        deleteList.push(element.id);
      }
    });

    if (deleteList.length == 0) {
      // 异常
      this._notification.create('warning', '警告提示', '请选中一条数据！');
      return;
    }

    const that = this;
    this.modalService.confirm({
      title  : '您是否确认要删除这项内容',
      content: '<b>一旦删除，无法还原</b>',
      onOk() {
        that.sureDel = true;
      },
      onCancel() {
        that.sureDel = false;
      }
    });
    

    if(this.sureDel){
        // let ob = this.httpServcie.deleteSystemAuthorById({'id':deleteList[0]}); //单条删除
        let ob  =  this.httpServcie.deleteSystemAuthor({'ids':deleteList});  //批量删除
        ob.subscribe(()=>{
          this._notification.create('success', '成功', '删除成功！');
          this.query();
          this.queryAll();
        },(error)=>{
          this._notification.create('error', '错误提示', '删除失败！');
        })
    }

     

   
  }
  

  //选择类型
  changeType(param) {
    this.sysAuthorBean.sysType = param;
  }
}